import http from 'wechat-http'

http.baseURL = 'https://live-api.itheima.net'
http.intercept.request = (config) => {
  const token = getApp().token
  if (token && !config.url.includes(`/refreshToken`)) {
    config.header = config.header || {}
    config.header.Authorization = token
  }
  return config
}
http.intercept.response = async (res) => {
  console.log(res)
  if (res.statusCode === 401) {
    const { code, data } = await wx.http({
      url: '/refreshToken',
      method: 'post',
      header: {
        Authorization: getApp().refreshToken

      }
    })
    console.log(res)
    if (code !== 10000) return wx.utils.toast
    getApp().setToken(data.token, data.refreshToken)
  }
  return res.data
}
wx.http = http
export default http